#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/9/7 09:43
# @Author : wn 
# @File: accountmapping.py
# @SoftWare: PyCharm
# @Site:
# @Table:
# ｡◕‿◕｡｡◕‿◕｡｡◕‿◕｡
from django.shortcuts import render,HttpResponse
from django.views import View

from Manager.utils.mypage import *
from Manager.utils.pageSearch import *
import json


class AccountMapping(View):
    def get(self, request):
        # 当前在第几页
        off = request.GET.get('offset', None)
        # 一页几条
        lim = request.GET.get('limit', None)

        # get请求修改页面字段值
        get_am_modify = request.GET.get('modify_am', None)
        if lim is None and get_am_modify is not None:
            am_list = list(Accountmapping.objects.filter(id=get_am_modify))
            for item in am_list:
                result_dict = {}
                result_dict['accid'] = item.accid  # str
                result_dict['accname'] = item.accname  # str
                result_dict['combino'] = item.combino  # str
                result_dict['combiname'] = item.combiname  # str
                result_dict['amoption'] = item.id
            result = my_page([result_dict], lim, off)
            return JsonResponse(result, safe=False)

        if lim is None:
            html = "accountmapping.html"
            return render(request, html, {'url': '/strategy/accountmapping'})

    def post(self, request):
        if request.POST.get('accountmapping_search', None):
            # 初始化页面数据，搜索
            data = json.loads(request.POST.get('data'))
            combino=data.get('combino',None)
            combiname=data.get('combiname',None)
            accid=data.get('accid',None)
            accname=data.get('accname',None)
            jsonRes = search_accountmapping(combino=combino, combiname=combiname, accid=accid, accname=accname)
            return jsonRes
        elif request.POST.get('del_DB', None):
            id = request.POST.get('id', None)  # str
            # 删除的时候可以用id标注唯一一条记录
            Accountmapping.objects.filter(id=id).delete()
            jsonRes = search_accountmapping()
            return jsonRes
        elif request.POST.get('addORmodify_DB', None):
            # 修改的时候的get可以用id表示唯一的一条记录，显示get值；
            # 修改的时候后台建议用联合主键，而非id，防止用户输入相同的combino，accid导致数据库报错。
            data = json.loads(request.POST.get('data'))
            combino = data.get('combino',None)
            combiname = data.get('combiname',None)
            accid = data.get('accid',None)
            accname = data.get('accname',None)
            aORmFlag = data.get('aORmFlag',None)
            if aORmFlag =='add':
                if Accountmapping.objects.filter(combino=combino, accid=accid).exists():
                    return HttpResponse('am')
                else:
                    Accountmapping.objects.create(
                        combino=combino,
                        combiname=combiname,
                        accid=accid,
                        accname=accname,
                    )
                jsonRes = search_accountmapping()
                return jsonRes
            else:
                if Accountmapping.objects.filter(combino=combino,accid=accid).exists():
                    Accountmapping.objects.filter(combino=combino,accid=accid).update(
                        combino=combino,
                        combiname=combiname,
                        accid=accid,
                        accname=accname,
                    )
                    jsonRes = search_accountmapping()
                    return jsonRes
